#region Copyright
//       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
// 
//        This program is free software: you can redistribute it and/or modify
//        it under the terms of the GNU General Public License as published by
//        the Free Software Foundation, either version 3 of the License, or
//        (at your option) any later version.
// 
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
// 
//        You should have received a copy of the GNU General Public License
//        along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using FT.Architecture.Entities;
using FT.Architecture.Queries;

namespace FT.Architecture.Repositories
{
    /// <summary>
    /// Repository Interface
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TId"></typeparam>
    public interface IGenericRepository<T,TId> where T : IGenericEntity<TId>
    {
        /// <summary>
        /// Get an entity by its ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T GetById(TId id);

        /// <summary>
        /// Get an entity by its ID, and fetch the specified members as well
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fetchExpressions"></param>
        /// <returns></returns>
        T GetById(TId id, params Expression<Func<T, object>>[] fetchExpressions);

        /// <summary>
        /// Gets a random entity. This is helpful for test purposes.
        /// </summary>
        /// <returns></returns>
        T GetRandom();

        /// <summary>
        /// Returns the next available (not taken) ID
        /// </summary>
        /// <returns></returns>
        TId GetNextId();

        /// <summary>
        /// Get the total count of entities
        /// </summary>
        /// <returns></returns>
        long GetCount();

        /// <summary>
        /// Get an entity by one of its member value. This has to return only one result.
        /// </summary>
        /// <param name="member"></param>
        /// <param name="value"></param>
        /// <param name="fetchExpressions"></param>
        /// <returns></returns>
        T GetByMember(Expression<Func<T, object>> member, object value, params Expression<Func<T, object>>[] fetchExpressions);

        /// <summary>
        /// Get an entity by two of its member value. This has to return only one result.
        /// </summary>
        /// <returns></returns>
        T GetByMember(Expression<Func<T, object>> member1, object value1, Expression<Func<T, object>> member2, object value2, params Expression<Func<T, object>>[] fetchExpressions);

        /// <summary>
        /// Get an entity by three of its member value. This has to return only one result.
        /// </summary>
        /// <returns></returns>
        T GetByMember(Expression<Func<T, object>> member1, object value1, Expression<Func<T, object>> member2, object value2, Expression<Func<T, object>> member3, object value3, params Expression<Func<T, object>>[] fetchExpressions);

        /// <summary>
        /// Get all entities of the specified type
        /// </summary>
        /// <returns></returns>
        IList<T> GetAll();

        /// <summary>
        /// Get all entities, and fetches the chosen members, using the provided delegate.
        /// You can even specify sub-items (i.e. item.SubItem.SubSubItem etc.)
        /// </summary>
        /// <param name="fetchMembers"></param>
        /// <returns></returns>
        IList<T> GetAll(params Expression<Func<T, object>>[] fetchMembers);

        /// <summary>
        /// Get all entities, and fetches the chosen members, using the provided delegate.
        /// You can even specify sub-items (i.e. item.SubItem.SubSubItem etc.).
        /// The results will be ordered by the specified member
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortAscending"></param>
        /// <param name="fetchMembers"></param>
        /// <returns></returns>
        IList<T> GetAll(Expression<Func<T, object>> orderBy, bool sortAscending, params Expression<Func<T, object>>[] fetchMembers);

        /// <summary>
        /// Get all entities, and fetches the chosen members, using the provided delegate.
        /// You can even specify sub-items (i.e. item.SubItem.SubSubItem etc.)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="fetchMembers"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        IList<T> GetAllByMember(Expression<Func<T, object>> member, object value, params Expression<Func<T, object>>[] fetchMembers);

        /// <summary>
        /// Get all entities, and fetches the chosen members, using the provided delegate.
        /// You can even specify sub-items (i.e. item.SubItem.SubSubItem etc.)
        /// This will be filtered by member1 and member2 values.
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <param name="fetchMembers"></param>
        /// <param name="member1"></param>
        /// <param name="member2"></param>
        /// <returns></returns>
        IList<T> GetAllByMember(Expression<Func<T, object>> member1, object value1, Expression<Func<T, object>> member2, object value2, params Expression<Func<T, object>>[] fetchMembers);

        /// <summary>
        /// Get all entities, and fetches the chosen members, using the provided delegate.
        /// You can even specify sub-items (i.e. item.SubItem.SubSubItem etc.)
        /// This will be filtered by member1, member2 and member3 values.
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <param name="value3"></param>
        /// <param name="fetchMembers"></param>
        /// <param name="member1"></param>
        /// <param name="member2"></param>
        /// <param name="member3"></param>
        /// <returns></returns>
        IList<T> GetAllByMember(Expression<Func<T, object>> member1, object value1, Expression<Func<T, object>> member2, object value2, Expression<Func<T, object>> member3, object value3, params Expression<Func<T, object>>[] fetchMembers);

        /// <summary>
        /// Perform a search using a Query object
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        IEnumerable<T> FindBy(Query<T> query);

        /// <summary>
        /// Return the number of results a given query would return
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        long CountBy(Query<T> query);

        /// <summary>
        /// Create a new query object
        /// </summary>
        /// <returns></returns>
        Query<T> CreateQuery();

        /// <summary>
        /// Get Linq provider
        /// </summary>
        /// <returns></returns>
        IQueryable<T> GetLinq();

        /// <summary>
        /// Save an entity: Make it persistent, as opposed to transient
        /// </summary>
        /// <param name="entity"></param>
        void Save(T entity);

        /// <summary>
        /// Batch saves a list of entities using a stateless session
        /// </summary>
        /// <param name="entities"></param>
        void BulkSave(IEnumerable<T> entities);

        /// <summary>
        /// Delete an entity: Make it transient again. This will reset the entity Id to its default value.
        /// </summary>
        /// <param name="entity"></param>
        void Delete(T entity);

        /// <summary>
        /// Delete an entity: Make it transient again.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="resetId">if true, the Id will be reset to its default value</param>
        void Delete(T entity, bool resetId);

        /// <summary>
        /// Delete a batch of entities. This will reset the entities Ids to their default values.
        /// </summary>
        /// <param name="entities"></param>
        void Delete(params T[] entities);

        /// <summary>
        /// Delete a batch of entities. This will reset the entities Ids to their default values.
        /// </summary>
        /// <param name="entities"></param>
        void Delete(IEnumerable<T> entities);

        /// <summary>
        /// Delete a batch of entities
        /// </summary>
        /// <param name="resetId">if true, the Id will be reset to its default value</param>
        /// <param name="entities"></param>
        void Delete(bool resetId, params T[] entities);

        /// <summary>
        /// Delete a batch of entities
        /// </summary>
        /// <param name="entities"></param>
        /// <param name="resetId">if true, the Id will be reset to its default value</param>
        void Delete(IEnumerable<T> entities, bool resetId);

        /// <summary>
        /// Un-proxy an object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="proxiedEntity"></param>
        /// <returns></returns>
        T UnProxy(T proxiedEntity);

        /// <summary>
        /// Get the concrete class of an object, from a proxied version
        /// </summary>
        /// <param name="proxiedEntity"></param>
        /// <returns></returns>
        Type GetConcreteType(T proxiedEntity);

        /// <summary>
        /// Detach an entity from its unit of work
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        T Detach(T entity);

        /// <summary>
        /// Reattach an entity to a current unit of work. 
        /// ALWAYS REPLACE YOUR REFERENCE TO THE ENTITY BY THE ONE RETURNED BY THIS METHOD
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="attachMode">Set the way the entity or the database will be updated if they differ</param>
        /// <returns></returns>
        T Attach(T entity, AttachMode attachMode);

        /// <summary>
        /// Gets information about the underlying storage
        /// </summary>
        IGenericUnderlyingStorage<TId, T> UnderlyingStorage { get; }
    }
}